package com.luckberry.murder.controller.api;

import com.alibaba.fastjson.JSON;
import com.luckberry.murder.entity.good.Good;
import com.luckberry.murder.service.GoodService;
import com.luckberry.murder.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApiGoodController {

    @Resource
    private GoodService service;

    @ResponseBody
    @RequestMapping(value = "/goods", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getGoods() {
        return JSON.toJSONString(service.getGoods());
    }

    @ResponseBody
    @RequestMapping(value = "/good/{gid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getGood(@PathVariable("gid") int gid) {
        return JSON.toJSONString(service.getGood(gid));
    }

    @ResponseBody
    @RequestMapping(value = "/good", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addGood(@RequestBody String json) {
        return JSONUtil.result(service.addGood(JSON.parseObject(json, Good.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/good/{gid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteGood(@PathVariable("gid") int gid) {
        return JSONUtil.result(service.deleteGood(gid));
    }

    @ResponseBody
    @RequestMapping(value = "/good", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editGood(@RequestBody String json) {
        return JSONUtil.result(service.editGood(JSON.parseObject(json, Good.class)));
    }
}
